import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";

interface UserState {
  username: string;
  nickname: string;
  avator: string;
  token: string;
  isLogin: boolean;
  loading: boolean;
}

const initialState: UserState = {
  username: "",
  nickname: "",
  avator: "",
  token: "",
  isLogin: false,
  loading: false,
};


export const login = createAsyncThunk(
  '/user/login',
  async (data) => {
    const resp = await axios.post('/api/login', data);
    return resp.data
  }
)
const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {},
  extraReducers: {
    [login.pending.type](state) {
      state.loading = true
    },
    [login.fulfilled.type](state, action) {
      state.isLogin = true
      state.loading = false
    },
    // [login.rejected.type](state, action) {
    //   state. = false
    // }
  }
});

export default userSlice;
